package com.bwie.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.domain.TbFriend;
import com.bwie.domain.TbUser;
import com.bwie.mapper.TbFriendMapper;
import com.bwie.service.TbFriendService;
import com.bwie.service.TbUserService;
import com.bwie.mapper.TbUserMapper;
import com.bwie.utils.MobileMessageUtil;
import com.bwie.utils.R;
import com.bwie.vo.TbUserVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* @author xinghe
* @description 针对表【tb_user(用户表)】的数据库操作Service实现
* @createDate 2025-06-17 13:33:47
*/
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser>
    implements TbUserService {
    @Autowired
    private TbUserMapper tbUserMapper;

    @Autowired
    private TbFriendMapper tbFriendMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    @Transactional
    public R login(TbUserVo tbUserVo) {

        TbUser tbUser = tbUserMapper.selectOne(
                new QueryWrapper<TbUser>().lambda().eq(TbUser::getUserName, tbUserVo.getUserName())
        );

        if(tbUser == null){
            return R.ERROR(500,"用户不存在");
        }
        if(!tbUser.getUserPass().equals(tbUserVo.getUserPass())){
            return R.ERROR(500,"密码错误");
        }


        String token = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, "123456")
                .claim("userId", tbUser.getUserId())
                .claim("userName", tbUser.getUserName())
                .compact();


        String tokenKey = "token-" + tbUser.getUserId();

        stringRedisTemplate.opsForValue().set(tokenKey,token,30, TimeUnit.MINUTES);


        List<TbFriend> list = tbFriendMapper.selectByUserId(tbUser.getUserId());

        String key = "tbFriend-" + tbUser.getUserId();

        stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(list),60,TimeUnit.MINUTES);

        tbUser.setToken(token);

        return R.OK(tbUser);
    }

    @Override
    public R sendCode(String phone) {

        String code = RandomUtil.randomNumbers(4);

        MobileMessageUtil.sendMobileMessage(phone,code);

        String codeKey = "phone-" + phone;

        stringRedisTemplate.opsForValue().set(codeKey,code,5,TimeUnit.MINUTES);

        return R.OK(code);
    }

    @Override
    public R registerUser(TbUserVo tbUserVo) {

        TbUser tbUser = tbUserMapper.selectOne(
                new QueryWrapper<TbUser>().lambda().eq(TbUser::getPhone, tbUserVo.getPhone())
        );

        if(tbUser != null){
            return R.ERROR(500,"手机号已注册");
        }

        String codeKey = "phone-" + tbUserVo.getPhone();

        String redisCode = stringRedisTemplate.opsForValue().get(codeKey);
        if(redisCode == null){
            return R.ERROR(500,"验证码已过期");
        }
        if(!redisCode.equals(tbUserVo.getCode())){
            return R.ERROR(500,"验证码错误");
        }

        TbUser tbUser1 = new TbUser();
        tbUser1.setUserName(tbUserVo.getUserName());
        tbUser1.setUserPass(tbUserVo.getUserPass());
        tbUser1.setPhone(tbUserVo.getPhone());
        tbUser1.setBirthday(tbUserVo.getBirthday());
        tbUser1.setAddress(tbUserVo.getAddress());
        tbUserMapper.insert(tbUser1);


        return R.OK();
    }

}




